package com.quarters.serviceImpl;

import com.quarters.bean.Notice;
import com.quarters.bean.User;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.request.SendArticles;
import com.quarters.bean.request.SendNotice;
import com.quarters.bean.request.VoPage;
import com.quarters.mapper.NoticeMapper;
import com.quarters.mapper.UserMapper;
import com.quarters.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quarters.service.XbxWxService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.quarters.serviceImpl.InquiryServiceImpl.stampToDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-11
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    XbxWxService xbxWxService;

    /**
     * @param voPage
     * @return 后台查询
     */
    @Override
    public ResultPage<Notice> selectMyPage(VoPage voPage, Long userInfoId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("userInfoId", userInfoId);
        List<Notice> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Notice> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public ResultPage<Notice> selectMyPageAll(VoPage voPage ) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<Notice> list = baseMapper.selectMyPageAll(paramMap);
        Integer count = baseMapper.countMyPageAll(paramMap);
        ResultPage<Notice> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * @param str
     * @return 去除html标签
     */
    public static String delHtmlTag(String str){
        String newstr = "";
        newstr = str.replaceAll("<[.[^>]]*>","");
        newstr = newstr.replaceAll(" ", "");
        return newstr;
    }

    /**
     * 发送通知模板消息
     */
    @Override
    public void sendNotice(String userId,Long createTime,String content,Long noticeId) {
        String userIds[] = userId.split(",");
        for (int i = 0; i < userIds.length; i++) {
            User user = userMapper.selectByIdUser(Long.parseLong(userIds[i]));
            //给用户发送通知
            SendNotice sendArticles = new SendNotice();

//            String content1 = delHtmlTag(content);
//            String content2 =  content1.replaceAll("<br\\s*/?>", "\r\n");
//            String content3 =  content2.replaceAll("<p .*?>", "\r\n");
//
//            System.out.println("content3:"+content3);

            //推送标题
            sendArticles.setContent(content);
            //创建时间
            sendArticles.setCreateTime(stampToDate(createTime + ""));
            //推送人
            sendArticles.setToUser(user.getOpenid());
            //通知id
            sendArticles.setNoticeId(noticeId);

            try {
                xbxWxService.sendNotice(sendArticles);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }
    }

}
